package com.letfox.auth.service.impl;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.letfox.auth.service.LoginService;
import com.letfox.common.enums.AuthEnum;
import com.letfox.common.enums.ValidateEnum;
import com.letfox.common.model.ResultResponse;
import com.letfox.common.util.ConvertTool;
import com.letfox.common.util.MD5Util;
import com.letfox.common.util.UUIDHexGenerator;
import com.letfox.user.enums.AccountEnums.AccountAuthState;
import com.letfox.user.enums.AccountEnums.AccountState;
import com.letfox.user.enums.UserEnums.UserAuthState;
import com.letfox.user.enums.UserEnums.UserState;
import com.letfox.user.model.UAccount;
import com.letfox.user.service.AccountService;

@Service
public class LoginServiceImpl implements LoginService {
	@Autowired
	private AccountService accountService;

	@Transactional(rollbackFor = Exception.class) 
	public ResultResponse createAccount(UAccount account) throws Exception {
		ResultResponse resultResponse=new ResultResponse();
		//检验账号
		if(StringUtils.isBlank(account.getAccount())){
			resultResponse.setStatusMessage(ValidateEnum.V100.getCode());
			resultResponse.setStatusMessage(ValidateEnum.V100.getMessage());
			return resultResponse;
		}
		//检验密码
		if(StringUtils.isBlank(account.getPassword())){
			resultResponse.setStatusMessage(ValidateEnum.V102.getCode());
			resultResponse.setStatusMessage(ValidateEnum.V102.getMessage());
			return resultResponse;
		}
		//检验账号 - 是否已注册
		UAccount accountInfo = accountService.getByAccount(account.getAccount());
		if(accountInfo != null){
			resultResponse.setStatusMessage(ValidateEnum.V106.getCode());
			resultResponse.setStatusMessage(ValidateEnum.V106.getMessage());
			return resultResponse;
		}
		account.setRegisterTime(new Date());
		account.setUpdateTime(new Date());
		account.setAccountId(UUIDHexGenerator.generator());
		//设置状态
		account.setState(AccountState.STARTUP.getValue());
		account.setAccountAuthState(AccountAuthState.ACTIVITY.getValue());
		try {
			accountService.save(account);
			resultResponse.setStatusCode(AuthEnum.R200.getCode());
			resultResponse.setStatusMessage(AuthEnum.R200.getMessage());
		} catch (Exception e) {
			throw new Exception("sql erro",e);
		}
		return resultResponse;
	}

	@Override
	public ResultResponse getLogin(String account, String password) throws Exception{
		ResultResponse resultResponse=new ResultResponse();
		String checkAccount = ConvertTool.toString(account);
		String checkPassword = ConvertTool.toString(password);
		//检验账号
		if(StringUtils.isBlank(checkAccount)){
			resultResponse.setStatusMessage(ValidateEnum.V100.getCode());
			resultResponse.setStatusMessage(ValidateEnum.V100.getMessage());
			return resultResponse;
		}
		//检验密码
		if(StringUtils.isBlank(checkPassword)){
			resultResponse.setStatusMessage(ValidateEnum.V102.getCode());
			resultResponse.setStatusMessage(ValidateEnum.V102.getMessage());
			return resultResponse;
		}
		UAccount accountInfo = null;
		//查找账号
		try {
			accountInfo = accountService.getByAccount(checkAccount);
		} catch (Exception e) {
			throw new Exception("sql erro",e);
		}
		
		//账号是否存在
		if(accountInfo == null){
			resultResponse.setStatusCode(ValidateEnum.V100.getCode());
			resultResponse.setStatusMessage(ValidateEnum.V100.getMessage());
			return resultResponse;
		}
		//再次检验密码
		if(!accountInfo.getPassword().equals(MD5Util.MD5(checkPassword))){
			resultResponse.setStatusCode(ValidateEnum.V101.getCode());
			resultResponse.setStatusMessage(ValidateEnum.V101.getMessage());
			return resultResponse; 
		}
		resultResponse.setStatusCode(AuthEnum.R200.getCode());
		resultResponse.setStatusMessage(AuthEnum.R200.getMessage());

		return resultResponse;
	}

}

